import 'dart:convert';

import 'package:fish_redux/fish_redux.dart';
import 'action.dart';
import 'state.dart';
import 'dart:io';
import '../../app_files.dart';
import 'model/order_view_model.dart';

Effect<OrderViewState> buildEffect() {
  return combineEffects(<Object, Effect<OrderViewState>>{
    OrderViewAction.onTapAddPic: _onTapAddPic,
    OrderViewAction.onSubmit: _onSubmit,
    OrderViewAction.onTapPic: _onTapPic,
    Lifecycle.initState: _initState,
  });
}

void _onTapPic(Action action, Context<OrderViewState> ctx) async {
  OrderConfigModel model = action.payload['model'];
  var file = action.payload['data'];
  int res =
  await CommonUtils.showActionSheet(ctx.context, button: ['查看', '删除']);
  if (res == 1) {
    model.imageList.remove(file);
    ctx.dispatch(OrderViewActionCreator.onAction());
  } else if (res == 0) {
    await CommonUtils.showMediaAlbumView(
      ctx.context,
      initialIndex: model.imageList.indexOf(file),
      files: model.imageList,
    );
  }
}

void _onTapAddPic(Action action, Context<OrderViewState> ctx) async {
  OrderConfigModel model = action.payload;
  AssetEntityFile imgFile = await CommonUtils.showOrderImagePicker(ctx.context,
      type: 0, info: ctx.state.userInfo);
  if (imgFile != null) {
    model.imageList.add(imgFile);
    ctx.dispatch(OrderViewActionCreator.onAction());
  }
}

void _initState(Action action, Context<OrderViewState> ctx) async {
  // ResultData info = await CommonNetWork.gasAccountInfo(
  //   accountNo: ctx.state.userInfo.userNo,
  //   userName: ctx.state.userInfo.userName,
  // );
  // if (info.success) {
  //   ctx.state.gasInfo.meterBaseVolumeController.text = CommonUtils.checkNullToString(info.data['MeterBaseVolume']);
  //   if (ctx.state.userInfo.isICGasUser) {
  //     ctx.state.gasInfo.gasRemainTotalController.text = ctx.state.gasInfo.gasBuyTotalController.text = CommonUtils.checkNullToString(info.data['UAmountTotalCubic']);
  //   }
  //   else {
  //     ctx.state.gasInfo.gasBuyTotalController.text =  CommonUtils.checkNullToString(info.data['TotalFee']);
  //     ctx.state.gasInfo.gasUseTotalController.text = CommonUtils.checkNullToString(info.data['UAmountChaobiaoNumber']);
  //     ctx.state.gasInfo.gasRemainTotalController.text =  CommonUtils.checkNullToString(info.data['UAmountBalance']);
  //   }
  //
  //   ctx.dispatch(OrderViewActionCreator.onAction());
  // }
  print('---------------------${ctx.state.userInfo.orderID}');
  ResultData res = await CommonNetWork.staffCheckConfig(
    worktplType: 1,
    worktplUsertype: ctx.state.userInfo.userType,
  );
  if (res.success) {
    Map data = res.data['data'];

    String listJson = data['worktplItem'];
    List list = jsonDecode(listJson);
    List<OrderConfigModel> items = list
        .map<OrderConfigModel>((e) => OrderConfigModel.fromConfigJson(e))
        .toList();
    ctx.dispatch(OrderViewActionCreator.onGetData(items));
  } else {
    CommonUtils.showErrorInfo(ctx.context, res.desc);
  }
}

void _onSubmit(Action action, Context<OrderViewState> ctx) async {
  String errStr;
  for (OrderConfigModel model in ctx.state.items) {
    if (model.values.length == 0 && !model.hasOther && model.isNormal != true) {
      errStr = '请选择<${model.configName}>检测内容';
      break;
    }

    if (model.hasOther && model.textEditingController.text.length == 0) {
      errStr = '请填写<${model.configName}>问题描述';
      break;
    }
  }
  //
  // if (ctx.state.gasInfo.gasRemainTotalStr.length == 0) errStr = '请填写剩余气量';
  // if (ctx.state.gasInfo.gasUseTotalStr.length == 0) errStr = '请填写总用气量';
  // if (ctx.state.gasInfo.gasBuyTotalStr.length == 0) errStr = '请填写总购气量';

  if (errStr != null) {
    CommonUtils.showAlertView(ctx.context, errStr);
  } else {
    CommonUtils.pushRoute(ctx.context, RoutePath.orderResultView, arguments: {
      'info': ctx.state.userInfo,
      'items': ctx.state.items,
      'gasInfo': ctx.state.gasInfo
    });
  }
}
